/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-6 下午5:55
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities.life;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.bean.life.LifeLearn;
import com.example.dqhl.jdmetro.fragment.life.LifeCommonFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:生活汇  学
 * autour:guanluocang
 * date:17/7/6 17:54
 */
public class LifeLearnActivity extends BaseActivity {

    private Context context;
    //距离排序
    @Bind(R.id.tv_distance)
    TextView tv_distance;
    @Bind(R.id.line_order_for_the_distance)
    View line_order_for_the_distance;

    //人气排序
    @Bind(R.id.tv_popularity)
    TextView tv_popularity;
    @Bind(R.id.line_order_to_be_populatity)
    View line_order_to_be_populatity;

    @Bind(R.id.learn_viewPager)
    ViewPager learn_viewPager;

    private List<Fragment> fragmentList;
    private LifeCommonFragment distanceFragment;
    private LifeCommonFragment populatityFragment;

    private List<LifeLearn> lifeLearnsList = new ArrayList<>();
    private Double latitude;
    private Double longitude;
    private String type = "";

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_life_learn);
        setRightTextVisibility(View.GONE);
        context = this;
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        latitude = getIntent().getDoubleExtra("latitude", 0);
        longitude = getIntent().getDoubleExtra("longitude", 0);
        type = getIntent().getStringExtra("type");
        if ("1".equals(type)) {
            initTitleBar(R.string.txt_learn);
        }else if("2".equals(type)){
            initTitleBar(R.string.txt_play);
        }else if("3".equals(type)){
            initTitleBar(R.string.txt_move);
        }else{
            initTitleBar(R.string.txt_shop);
        }

        fragmentList = new ArrayList<>();

        distanceFragment = new LifeCommonFragment();
        Bundle bundle_distance = new Bundle();
        bundle_distance.putString("state", "distance");
        bundle_distance.putDouble("latitude", latitude);
        bundle_distance.putDouble("longitude", longitude);
        bundle_distance.putString("type", type);
        distanceFragment.setArguments(bundle_distance);

        populatityFragment = new LifeCommonFragment();
        Bundle bundle_populatity = new Bundle();
        bundle_populatity.putString("state", "populatity");
        bundle_populatity.putDouble("latitude", latitude);
        bundle_populatity.putDouble("longitude", longitude);
        bundle_populatity.putString("type", type);
        populatityFragment.setArguments(bundle_populatity);

        fragmentList.add(distanceFragment);
        fragmentList.add(populatityFragment);

        learn_viewPager.setAdapter(new LearnFragmentPagerAdapter(getSupportFragmentManager()));
        learn_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        line_order_for_the_distance.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
                        line_order_to_be_populatity.setBackgroundColor(ContextCompat.getColor(context, R.color.color_white));
                        break;
                    case 1:
                        line_order_for_the_distance.setBackgroundColor(ContextCompat.getColor(context, R.color.color_white));
                        line_order_to_be_populatity.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public void initListener() {
        tv_distance.setOnClickListener(this);
        tv_popularity.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //距离
            case R.id.tv_distance:
                learn_viewPager.setCurrentItem(0, true);
                break;
            //人气
            case R.id.tv_popularity:
                learn_viewPager.setCurrentItem(1, true);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    class LearnFragmentPagerAdapter extends FragmentPagerAdapter {

        public LearnFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }


    }
}
